<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_Http_Client - Connection Adapters - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.http.client.adapters.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.http.client.adapters.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.http.client.advanced.html">Zend_Http_Client - Advanced Usage</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.http.html">Zend_Http</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.http.cookies.html">Zend_Http_Cookie and Zend_Http_CookieJar</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.http.client.adapters" class="section"><div class="info"><h1 class="title">Zend_Http_Client - Connection Adapters</h1></div>
    

    <div class="section" id="zend.http.client.adapters.overview"><div class="info"><h1 class="title">Overview</h1></div>
        

        <p class="para">
            <span class="classname">Zend_Http_Client</span> is based on a connection adapter design. The
            connection adapter is the object in charge of performing the
            actual connection to the server, as well as writing requests
            and reading responses.
            This connection adapter can be replaced, and you can create and
            extend the default connection adapters to suite your special needs,
            without the need to extend or replace the entire <acronym class="acronym">HTTP</acronym> client
            class, and with the same interface.
        </p>

        <p class="para">
            Currently, the <span class="classname">Zend_Http_Client</span> class provides four built-in
            connection adapters:

            <ul class="itemizedlist">
                <li class="listitem">
                    <p class="para">
                        <span class="classname">Zend_Http_Client_Adapter_Socket</span> (default)
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <span class="classname">Zend_Http_Client_Adapter_Proxy</span>
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <span class="classname">Zend_Http_Client_Adapter_Curl</span>
                    </p>
                </li>

                <li class="listitem">
                    <p class="para">
                        <span class="classname">Zend_Http_Client_Adapter_Test</span>
                    </p>
                </li>
            </ul>
        </p>

        <p class="para">
            The <span class="classname">Zend_Http_Client</span> object&#039;s adapter connection adapter is set
            using the &#039;adapter&#039; configuration option. When instantiating the
            client object, you can set the &#039;adapter&#039; configuration option to
            a string containing the adapter&#039;s name (eg. &#039;Zend_Http_Client_Adapter_Socket&#039;)
            or to a variable holding an adapter object (eg.
            <strong class="command">new Zend_Http_Client_Adapter_Test</strong>). You can also set the
            adapter later, using the <span class="classname">Zend_Http_Client-&gt;setConfig()</span> method.
        </p>
    </div>

    <div class="section" id="zend.http.client.adapters.socket"><div class="info"><h1 class="title">The Socket Adapter</h1></div>
        

        <p class="para">
            The default connection adapter is the
            <span class="classname">Zend_Http_Client_Adapter_Socket</span> adapter - this adapter will be
            used unless you explicitly set the connection adapter. The Socket adapter is based on
            <acronym class="acronym">PHP</acronym>&#039;s built-in fsockopen() function, and does not require any special
            extensions or compilation flags.
        </p>

        <p class="para">
            The Socket adapter allows several extra configuration options that
            can be set using <span class="classname">Zend_Http_Client-&gt;setConfig()</span> or
            passed to the client constructor.

            <table id="zend.http.client.adapter.socket.configuration.table" class="doctable table"><div class="info"><caption><b>Zend_Http_Client_Adapter_Socket configuration parameters</b></caption></div>
                

                
                    <thead valign="middle">
                        <tr valign="middle">
                            <th>Parameter</th>
                            <th>Description</th>
                            <th>Expected Type</th>
                            <th>Default Value</th>
                        </tr>

                    </thead>


                    <tbody valign="middle" class="tbody">
                        <tr valign="middle">
                            <td align="left">persistent</td>

                            <td align="left">
                                Whether to use persistent <acronym class="acronym">TCP</acronym> connections
                            </td>

                            <td align="left">boolean</td>
                            <td align="left"><b><tt>FALSE</tt></b></td>
                        </tr>


                        <tr valign="middle">
                            <td align="left">ssltransport</td>
                            <td align="left">SSL transport layer (eg. &#039;sslv2&#039;, &#039;tls&#039;)</td>
                            <td align="left">string</td>
                            <td align="left">ssl</td>
                        </tr>


                        <tr valign="middle">
                            <td align="left">sslcert</td>

                            <td align="left">
                                Path to a <acronym class="acronym">PEM</acronym> encoded <acronym class="acronym">SSL</acronym>
                                certificate
                            </td>

                            <td align="left">string</td>
                            <td align="left"><b><tt>NULL</tt></b></td>
                        </tr>


                        <tr valign="middle">
                            <td align="left">sslpassphrase</td>

                            <td align="left">
                                Passphrase for the <acronym class="acronym">SSL</acronym> certificate file
                            </td>

                            <td align="left">string</td>
                            <td align="left"><b><tt>NULL</tt></b></td>
                        </tr>


                        <tr valign="middle">
                            <td align="left">sslusecontext</td>

                            <td align="left">
                                Enables proxied connections to use SSL even if
                                the proxy connection itself does not.
                            </td>

                            <td align="left">boolean</td>
                            <td align="left"><b><tt>FALSE</tt></b></td>
                        </tr>

                    </tbody>
                
            </table>


            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>Persistent TCP Connections</b><br /></span>
                

                <p class="para">
                    Using persistent <acronym class="acronym">TCP</acronym> connections can potentially speed up
                    <acronym class="acronym">HTTP</acronym> requests - but in most use cases, will have little
                    positive effect and might overload the <acronym class="acronym">HTTP</acronym> server you are
                    connecting to.
                </p>

                <p class="para">
                    It is recommended to use persistent <acronym class="acronym">TCP</acronym> connections only if
                    you connect to the same server very frequently, and are
                    sure that the server is capable of handling a large number
                    of concurrent connections. In any case you are encouraged
                    to benchmark the effect of persistent connections on both
                    the client speed and server load before using this option.
                </p>

                <p class="para">
                    Additionally, when using persistent connections it is recommended to enable
                    Keep-Alive <acronym class="acronym">HTTP</acronym> requests as described in <a href="zend.http.client.html#zend.http.client.configuration" class="link">the configuration section</a> -
                    otherwise persistent connections might have little or no effect.
                </p>
            </p></blockquote>

            <blockquote class="note"><p><b class="note">Note</b>: <span class="info"><b>HTTPS SSL Stream Parameters</b><br /></span>
                

                <p class="para">
                    <span class="property">ssltransport</span>, <span class="property">sslcert</span> and
                    <span class="property">sslpassphrase</span> are only relevant when connecting using
                    <acronym class="acronym">HTTPS</acronym>.
                </p>

                <p class="para">
                    While the default <acronym class="acronym">SSL</acronym> settings should work for most
                    applications, you might need to change them if the server
                    you are connecting to requires special client setup. If so,
                    you should read the sections about <acronym class="acronym">SSL</acronym> transport layers and
                    options <a href="http://www.php.net/manual/en/transports.php#transports.inet" class="link external">&raquo; here</a>.
                </p>
            </p></blockquote>
        </p>

        <div class="example" id="zend.http.client.adapters.socket.example-1"><div class="info"><p><b>Example #1 Changing the HTTPS transport layer</b></p></div>
            

            <pre class="programlisting brush: php">
// Set the configuration parameters
$config = array(
    &#039;adapter&#039;      =&gt; &#039;Zend_Http_Client_Adapter_Socket&#039;,
    &#039;ssltransport&#039; =&gt; &#039;tls&#039;
);

// Instantiate a client object
$client = new Zend_Http_Client(&#039;https://www.example.com&#039;, $config);

// The following request will be sent over a TLS secure connection.
$response = $client-&gt;request();
</pre>

        </div>

        <p class="para">
            The result of the example above will be similar to opening a <acronym class="acronym">TCP</acronym>
            connection using the following <acronym class="acronym">PHP</acronym> command:
        </p>

        <p class="para">
             <span class="methodname">fsockopen(&#039;tls://www.example.com&#039;, 443)</span>
        </p>

        <div class="section" id="zend.http.client.adapters.socket.streamcontext"><div class="info"><h1 class="title">Customizing and accessing the Socket adapter stream context</h1></div>
            

            <p class="para">
                Starting from Zend Framework 1.9,
                <span class="classname">Zend_Http_Client_Adapter_Socket</span> provides direct access to the
                underlying <a href="http://php.net/manual/en/stream.contexts.php" class="link external">&raquo; stream context</a> used
                to connect to the remote server. This allows the user to pass specific options and
                parameters to the <acronym class="acronym">TCP</acronym> stream, and to the <acronym class="acronym">SSL</acronym>
                wrapper in case of <acronym class="acronym">HTTPS</acronym> connections.
            </p>

            <p class="para">
                You can access the stream context using the following methods of
                <span class="classname">Zend_Http_Client_Adapter_Socket</span>:

                <ul class="itemizedlist">
                    <li class="listitem">
                        <p class="para">
                            <b class="firstterm"> <span class="methodname">setStreamContext($context)</span></b>
                            Sets the stream context to be used by the adapter. Can accept either
                            a stream context resource created using the <a href="http://php.net/manual/en/function.stream-context-create.php" class="link external">&raquo;  <span class="methodname">stream_context_create()</span></a>
                            <acronym class="acronym">PHP</acronym> function, or an array of stream context options,
                            in the same format provided to this function. Providing an array will
                            create a new stream context using these options, and set it.
                        </p>
                    </li>

                    <li class="listitem">
                        <p class="para">
                            <b class="firstterm"> <span class="methodname">getStreamContext()</span></b>
                            Get the stream context of the adapter. If no stream context was set,
                            will create a default stream context and return it. You can then set
                            or get the value of different context options using regular
                            <acronym class="acronym">PHP</acronym> stream context functions.
                        </p>
                    </li>
                </ul>
            </p>

            <div class="example" id="zend.http.client.adapters.socket.streamcontext.example-1"><div class="info"><p><b>Example #2 Setting stream context options for the Socket adapter</b></p></div>
                

                <pre class="programlisting brush: php">
// Array of options
$options = array(
    &#039;socket&#039; =&gt; array(
        // Bind local socket side to a specific interface
        &#039;bindto&#039; =&gt; &#039;10.1.2.3:50505&#039;
    ),
    &#039;ssl&#039; =&gt; array(
        // Verify server side certificate,
        // do not accept invalid or self-signed SSL certificates
        &#039;verify_peer&#039; =&gt; true,
        &#039;allow_self_signed&#039; =&gt; false,

        // Capture the peer&#039;s certificate
        &#039;capture_peer_cert&#039; =&gt; true
    )
);

// Create an adapter object and attach it to the HTTP client
$adapter = new Zend_Http_Client_Adapter_Socket();
$client = new Zend_Http_Client();
$client-&gt;setAdapter($adapter);

// Method 1: pass the options array to setStreamContext()
$adapter-&gt;setStreamContext($options);

// Method 2: create a stream context and pass it to setStreamContext()
$context = stream_context_create($options);
$adapter-&gt;setStreamContext($context);

// Method 3: get the default stream context and set the options on it
$context = $adapter-&gt;getStreamContext();
stream_context_set_option($context, $options);

// Now, preform the request
$response = $client-&gt;request();

// If everything went well, you can now access the context again
$opts = stream_context_get_options($adapter-&gt;getStreamContext());
echo $opts[&#039;ssl&#039;][&#039;peer_certificate&#039;];
</pre>

            </div>

            <blockquote class="note"><p><b class="note">Note</b>: 
                <p class="para">
                    Note that you must set any stream context options before using the adapter
                    to preform actual requests. If no context is set before preforming
                    <acronym class="acronym">HTTP</acronym> requests with the Socket adapter, a default stream
                    context will be created. This context resource could be accessed after
                    preforming any requests using the  <span class="methodname">getStreamContext()</span>
                    method.
                </p>
            </p></blockquote>
        </div>
    </div>

    <div class="section" id="zend.http.client.adapters.proxy"><div class="info"><h1 class="title">The Proxy Adapter</h1></div>
        

        <p class="para">
            The <span class="classname">Zend_Http_Client_Adapter_Proxy</span> adapter is similar to the
            default Socket adapter - only the connection is made through an <acronym class="acronym">HTTP</acronym>
            proxy server instead of a direct connection to the target server. This
            allows usage of <span class="classname">Zend_Http_Client</span> behind proxy servers - which is
            sometimes needed for security or performance reasons.
        </p>

        <p class="para">
            Using the Proxy adapter requires several additional configuration
            parameters to be set, in addition to the default &#039;adapter&#039; option:

            <table id="zend.http.client.adapters.proxy.table" class="doctable table"><div class="info"><caption><b>Zend_Http_Client configuration parameters</b></caption></div>
                

                
                    <thead valign="middle">
                        <tr valign="middle">
                            <th>Parameter</th>
                            <th>Description</th>
                            <th>Expected Type</th>
                            <th>Example Value</th>
                        </tr>

                    </thead>


                    <tbody valign="middle" class="tbody">
                        <tr valign="middle">
                            <td align="left">proxy_host</td>
                            <td align="left">Proxy server address</td>
                            <td align="left">string</td>
                            <td align="left">&#039;proxy.myhost.com&#039; or &#039;10.1.2.3&#039;</td>
                        </tr>


                        <tr valign="middle">
                            <td align="left">proxy_port</td>
                            <td align="left">Proxy server <acronym class="acronym">TCP</acronym> port</td>
                            <td align="left">integer</td>
                            <td align="left">8080 (default) or 81</td>
                        </tr>


                        <tr valign="middle">
                            <td align="left">proxy_user</td>
                            <td align="left">Proxy user name, if required</td>
                            <td align="left">string</td>
                            <td align="left">&#039;shahar&#039; or &#039;&#039; for none (default)</td>
                        </tr>


                        <tr valign="middle">
                            <td align="left">proxy_pass</td>
                            <td align="left">Proxy password, if required</td>
                            <td align="left">string</td>
                            <td align="left">&#039;secret&#039; or &#039;&#039; for none (default)</td>
                        </tr>


                        <tr valign="middle">
                            <td align="left">proxy_auth</td>
                            <td align="left">Proxy <acronym class="acronym">HTTP</acronym> authentication type</td>
                            <td align="left">string</td>
                            <td align="left">Zend_Http_Client::AUTH_BASIC (default)</td>
                        </tr>

                    </tbody>
                
            </table>

        </p>

        <p class="para">
            proxy_host should always be set - if it is not set, the client will
            fall back to a direct connection using
            <span class="classname">Zend_Http_Client_Adapter_Socket</span>. proxy_port defaults to &#039;8080&#039; -
            if your proxy listens on a different port you must set this one as well.
        </p>

        <p class="para">
            proxy_user and proxy_pass are only required if your proxy server
            requires you to authenticate. Providing these will add a &#039;Proxy-Authentication&#039;
            header to the request. If your proxy does not require authentication,
            you can leave these two options out.
        </p>

        <p class="para">
            proxy_auth sets the proxy authentication type, if your proxy server
            requires authentication. Possibly values are similar to the ones
            accepted by the Zend_Http_Client::setAuth() method. Currently, only
            basic authentication (Zend_Http_Client::AUTH_BASIC) is supported.
        </p>

        <div class="example" id="zend.http.client.adapters.proxy.example-1"><div class="info"><p><b>Example #3 Using Zend_Http_Client behind a proxy server</b></p></div>
            

            <pre class="programlisting brush: php">
// Set the configuration parameters
$config = array(
    &#039;adapter&#039;    =&gt; &#039;Zend_Http_Client_Adapter_Proxy&#039;,
    &#039;proxy_host&#039; =&gt; &#039;proxy.int.zend.com&#039;,
    &#039;proxy_port&#039; =&gt; 8000,
    &#039;proxy_user&#039; =&gt; &#039;shahar.e&#039;,
    &#039;proxy_pass&#039; =&gt; &#039;bananashaped&#039;
);

// Instantiate a client object
$client = new Zend_Http_Client(&#039;http://www.example.com&#039;, $config);

// Continue working...
</pre>

        </div>

        <p class="para">
            As mentioned, if proxy_host is not set or is set to a blank string,
            the connection will fall back to a regular direct connection. This
            allows you to easily write your application in a way that allows a
            proxy to be used optionally, according to a configuration parameter.
        </p>

        <blockquote class="note"><p><b class="note">Note</b>: 
            <p class="para">
                Since the proxy adapter inherits from
                <span class="classname">Zend_Http_Client_Adapter_Socket</span>, you can use the stream
                context access method (see <a href="zend.http.client.adapters.html#zend.http.client.adapters.socket.streamcontext" class="link">this section</a>)
                to set stream context options on Proxy connections as demonstrated above.
            </p>
        </p></blockquote>
    </div>

    <div class="section" id="zend.http.client.adapters.curl"><div class="info"><h1 class="title">The cURL Adapter</h1></div>
        

        <p class="para">
            cURL is a standard <acronym class="acronym">HTTP</acronym> client library that is distributed with many
            operating systems and can be used in <acronym class="acronym">PHP</acronym> via the cURL extension. It
            offers functionality for many special cases which can occur for a
            <acronym class="acronym">HTTP</acronym> client and make it a perfect choice for a
            <acronym class="acronym">HTTP</acronym> adapter. It supports secure connections, proxy, all sorts of
            authentication mechanisms and shines in applications that move large files around
            between servers.
        </p>

        <div class="example" id="zend.http.client.adapters.curl.example-1"><div class="info"><p><b>Example #4 Setting cURL options</b></p></div>
            

            <pre class="programlisting brush: php">
$config = array(
    &#039;adapter&#039;   =&gt; &#039;Zend_Http_Client_Adapter_Curl&#039;,
    &#039;curloptions&#039; =&gt; array(CURLOPT_FOLLOWLOCATION =&gt; true),
);
$client = new Zend_Http_Client($uri, $config);
</pre>

        </div>

        <p class="para">
            By default the cURL adapter is configured to behave exactly like
            the Socket Adapter and it also accepts the same configuration parameters
            as the Socket and Proxy adapters. You can also change the cURL options by either
            specifying the &#039;curloptions&#039; key in the constructor of the adapter or by calling
             <span class="methodname">setCurlOption($name, $value)</span>. The <var class="varname">$name</var> key
            corresponds to the CURL_* constants of the cURL extension. You can
            get access to the Curl handle by calling <strong class="command">$adapter-&gt;getHandle();</strong>
        </p>

        <div class="example" id="zend.http.client.adapters.curl.example-2"><div class="info"><p><b>Example #5 Transfering Files by Handle</b></p></div>
            

            <div class="example-contents"><p>
                You can use cURL to transfer very large files over <acronym class="acronym">HTTP</acronym> by
                filehandle.
            </p></div>

            <pre class="programlisting brush: php">
$putFileSize   = filesize(&quot;filepath&quot;);
$putFileHandle = fopen(&quot;filepath&quot;, &quot;r&quot;);

$adapter = new Zend_Http_Client_Adapter_Curl();
$client = new Zend_Http_Client();
$client-&gt;setAdapter($adapter);
$adapter-&gt;setConfig(array(
    &#039;curloptions&#039; =&gt; array(
        CURLOPT_INFILE =&gt; $putFileHandle,
        CURLOPT_INFILESIZE =&gt; $putFileSize
    )
));
$client-&gt;request(&quot;PUT&quot;);
</pre>

        </div>
    </div>

    <div class="section" id="zend.http.client.adapters.test"><div class="info"><h1 class="title">The Test Adapter</h1></div>
        

        <p class="para">
            Sometimes, it is very hard to test code that relies on <acronym class="acronym">HTTP</acronym>
            connections. For example, testing an application that pulls an <acronym class="acronym">RSS</acronym>
            feed from a remote server will require a network connection, which is not always
            available.
        </p>

        <p class="para">
            For this reason, the <span class="classname">Zend_Http_Client_Adapter_Test</span> adapter is
            provided. You can write your application to use <span class="classname">Zend_Http_Client</span>,
            and just for testing purposes, for example in your unit testing
            suite, you can replace the default adapter with a Test adapter (a
            mock object), allowing you to run tests without actually
            performing server connections.
        </p>

        <p class="para">
            The <span class="classname">Zend_Http_Client_Adapter_Test</span> adapter provides an additional
            method, setResponse() method. This method takes one parameter,
            which represents an <acronym class="acronym">HTTP</acronym> response as either text or a
            <span class="classname">Zend_Http_Response</span> object. Once set, your Test adapter will
            always return this response, without even performing an actual <acronym class="acronym">HTTP</acronym>
            request.
        </p>

        <div class="example" id="zend.http.client.adapters.test.example-1"><div class="info"><p><b>Example #6 Testing Against a Single HTTP Response Stub</b></p></div>
            

            <pre class="programlisting brush: php">
// Instantiate a new adapter and client
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client(&#039;http://www.example.com&#039;, array(
    &#039;adapter&#039; =&gt; $adapter
));

// Set the expected response
$adapter-&gt;setResponse(
    &quot;HTTP/1.1 200 OK&quot;        . &quot;\r\n&quot; .
    &quot;Content-type: text/xml&quot; . &quot;\r\n&quot; .
                               &quot;\r\n&quot; .
    &#039;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#039; .
    &#039;&lt;rss version=&quot;2.0&quot; &#039; .
    &#039;     xmlns:content=&quot;http://purl.org/rss/1.0/modules/content/&quot;&#039; .
    &#039;     xmlns:wfw=&quot;http://wellformedweb.org/CommentAPI/&quot;&#039; .
    &#039;     xmlns:dc=&quot;http://purl.org/dc/elements/1.1/&quot;&gt;&#039; .
    &#039;  &lt;channel&gt;&#039; .
    &#039;    &lt;title&gt;Premature Optimization&lt;/title&gt;&#039; .
    // and so on...
    &#039;&lt;/rss&gt;&#039;);

$response = $client-&gt;request(&#039;GET&#039;);
// .. continue parsing $response..
</pre>

        </div>

        <p class="para">
            The above example shows how you can preset your <acronym class="acronym">HTTP</acronym> client to
            return the response you need. Then, you can continue testing your
            own code, without being dependent on a network connection, the server&#039;s
            response, etc. In this case, the test would continue to check how
            the application parses the <acronym class="acronym">XML</acronym> in the response body.
        </p>

        <p class="para">
            Sometimes, a single method call to an object can result in that
            object performing multiple <acronym class="acronym">HTTP</acronym> transactions. In this case, it&#039;s
            not possible to use setResponse() alone because there&#039;s no
            opportunity to set the next response(s) your program might need
            before returning to the caller.
        </p>

        <div class="example" id="zend.http.client.adapters.test.example-2"><div class="info"><p><b>Example #7 Testing Against Multiple HTTP Response Stubs</b></p></div>
            

            <pre class="programlisting brush: php">
// Instantiate a new adapter and client
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client(&#039;http://www.example.com&#039;, array(
    &#039;adapter&#039; =&gt; $adapter
));

// Set the first expected response
$adapter-&gt;setResponse(
    &quot;HTTP/1.1 302 Found&quot;      . &quot;\r\n&quot; .
    &quot;Location: /&quot;             . &quot;\r\n&quot; .
    &quot;Content-Type: text/html&quot; . &quot;\r\n&quot; .
                                &quot;\r\n&quot; .
    &#039;&lt;html&gt;&#039; .
    &#039;  &lt;head&gt;&lt;title&gt;Moved&lt;/title&gt;&lt;/head&gt;&#039; .
    &#039;  &lt;body&gt;&lt;p&gt;This page has moved.&lt;/p&gt;&lt;/body&gt;&#039; .
    &#039;&lt;/html&gt;&#039;);

// Set the next successive response
$adapter-&gt;addResponse(
    &quot;HTTP/1.1 200 OK&quot;         . &quot;\r\n&quot; .
    &quot;Content-Type: text/html&quot; . &quot;\r\n&quot; .
                                &quot;\r\n&quot; .
    &#039;&lt;html&gt;&#039; .
    &#039;  &lt;head&gt;&lt;title&gt;My Pet Store Home Page&lt;/title&gt;&lt;/head&gt;&#039; .
    &#039;  &lt;body&gt;&lt;p&gt;...&lt;/p&gt;&lt;/body&gt;&#039; .
    &#039;&lt;/html&gt;&#039;);

// inject the http client object ($client) into your object
// being tested and then test your object&#039;s behavior below
</pre>

        </div>

        <p class="para">
            The setResponse() method clears any responses in the
            <span class="classname">Zend_Http_Client_Adapter_Test</span>&#039;s buffer and sets the
            first response that will be returned. The addResponse()
            method will add successive responses.
        </p>

        <p class="para">
            The responses will be replayed in the order that they
            were added. If more requests are made than the number
            of responses stored, the responses will cycle again
            in order.
        </p>

        <p class="para">
            In the example above, the adapter is configured to test your
            object&#039;s behavior when it encounters a 302 redirect. Depending on
            your application, following a redirect may or may not be desired
            behavior. In our example, we expect that the redirect will be
            followed and we configure the test adapter to help us test this.
            The initial 302 response is set up with the setResponse() method
            and the 200 response to be returned next is added with the
            addResponse() method. After configuring the test adapter, inject
            the <acronym class="acronym">HTTP</acronym> client containing the adapter into your object under test
            and test its behavior.
        </p>

        <p class="para">
            If you need the adapter to fail on demand you can use
             <span class="methodname">setNextRequestWillFail($flag)</span>. The method will cause the next
            call to  <span class="methodname">connect()</span> to throw an
            <span class="classname">Zend_Http_Client_Adapter_Exception</span> exception. This can be useful
            when your application caches content from an external site (in case the site goes down)
            and you want to test this feature.
        </p>

        <div class="example" id="zend.http.client.adapters.test.example-3"><div class="info"><p><b>Example #8 Forcing the adapter to fail</b></p></div>
            

            <pre class="programlisting brush: php">
// Instantiate a new adapter and client
$adapter = new Zend_Http_Client_Adapter_Test();
$client = new Zend_Http_Client(&#039;http://www.example.com&#039;, array(
    &#039;adapter&#039; =&gt; $adapter
));

// Force the next request to fail with an exception
$adapter-&gt;setNextRequestWillFail(true);

try {
    // This call will result in a Zend_Http_Client_Adapter_Exception
    $client-&gt;request();
} catch (Zend_Http_Client_Adapter_Exception $e) {
    // ...
}

// Further requests will work as expected until
// you call setNextRequestWillFail(true) again
</pre>

        </div>
    </div>

    <div class="section" id="zend.http.client.adapters.extending"><div class="info"><h1 class="title">Creating your own connection adapters</h1></div>
        

        <p class="para">
            You can create your own connection adapters and use them. You could, for
            example, create a connection adapter that uses persistent sockets,
            or a connection adapter with caching abilities, and use them as
            needed in your application.
        </p>

        <p class="para">
            In order to do so, you must create your own adapter class that implements
            the <span class="classname">Zend_Http_Client_Adapter_Interface</span> interface. The following
            example shows the skeleton of a user-implemented adapter class. All the public
            functions defined in this example must be defined in your adapter as well:
        </p>

        <div class="example" id="zend.http.client.adapters.extending.example-1"><div class="info"><p><b>Example #9 Creating your own connection adapter</b></p></div>
            

            <pre class="programlisting brush: php">
class MyApp_Http_Client_Adapter_BananaProtocol
    implements Zend_Http_Client_Adapter_Interface
{
    /**
     * Set the configuration array for the adapter
     *
     * @param array $config
     */
    public function setConfig($config = array())
    {
        // This rarely changes - you should usually copy the
        // implementation in Zend_Http_Client_Adapter_Socket.
    }

    /**
     * Connect to the remote server
     *
     * @param string  $host
     * @param int     $port
     * @param boolean $secure
     */
    public function connect($host, $port = 80, $secure = false)
    {
        // Set up the connection to the remote server
    }

    /**
     * Send request to the remote server
     *
     * @param string        $method
     * @param Zend_Uri_Http $url
     * @param string        $http_ver
     * @param array         $headers
     * @param string        $body
     * @return string Request as text
     */
    public function write($method,
                          $url,
                          $http_ver = &#039;1.1&#039;,
                          $headers = array(),
                          $body = &#039;&#039;)
    {
        // Send request to the remote server.
        // This function is expected to return the full request
        // (headers and body) as a string
    }

    /**
     * Read response from server
     *
     * @return string
     */
    public function read()
    {
        // Read response from remote server and return it as a string
    }

    /**
     * Close the connection to the server
     *
     */
    public function close()
    {
        // Close the connection to the remote server - called last.
    }
}

// Then, you could use this adapter:
$client = new Zend_Http_Client(array(
    &#039;adapter&#039; =&gt; &#039;MyApp_Http_Client_Adapter_BananaProtocol&#039;
));
</pre>

        </div>
    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.http.client.advanced.html">Zend_Http_Client - Advanced Usage</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.http.html">Zend_Http</a></span><br />
                        <span class="home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.http.cookies.html">Zend_Http_Cookie and Zend_Http_CookieJar</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="manual.html">Guia de Refer&ecirc;ncia do Programador</a></li>
  <li class="header up"><a href="reference.html">Refer&ecirc;ncia do Zend Framework</a></li>
  <li class="header up"><a href="zend.http.html">Zend_Http</a></li>
  <li><a href="zend.http.client.html">Introduction</a></li>
  <li><a href="zend.http.client.advanced.html">Zend_Http_Client - Advanced Usage</a></li>
  <li class="active"><a href="zend.http.client.adapters.html">Zend_Http_Client - Connection Adapters</a></li>
  <li><a href="zend.http.cookies.html">Zend_Http_Cookie and Zend_Http_CookieJar</a></li>
  <li><a href="zend.http.response.html">Zend_Http_Response</a></li>
  <li><a href="zend.http.user-agent.html">Zend_Http_UserAgent</a></li>
  <li><a href="zend.http.user-agent-device.html">The UserAgent Device Interface</a></li>
  <li><a href="zend.http.user-agent-features.html">The UserAgent Features Adapter</a></li>
  <li><a href="zend.http.user-agent-features-wurfl.html">The WURFL UserAgent Features Adapter</a></li>
  <li><a href="zend.http.user-agent-features-device-atlas.html">The DeviceAtlas UserAgent Features Adapter</a></li>
  <li><a href="zend.http.user-agent-features-tera-wurfl.html">The TeraWurfl UserAgent Features Adapter</a></li>
  <li><a href="zend.http.user-agent-storage.html">The UserAgent Storage Interface</a></li>
  <li><a href="zend.http.user-agent-storage-session.html">The Session UserAgent Storage Adapter</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>